<template>
  <div class="home-container">
    <!-- 头部导航 -->
    <div class="header">
      <van-search @clear="value = ''" v-model="value" show-action placeholder="请输入搜索关键词" @search="onSearch" shape="round">
        <template #action>
          <div @click="onSearch" class="search-btn">搜索</div>
        </template>
      </van-search>
    </div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <!-- 轮播图 -->
      <div class="swiper" style="min-height: 100vw">
        <van-swipe :autoplay="3000" indicator-color="#ee0a24">
          <van-swipe-item v-for="s in swiper" :key="s.id" @click="toDetail(s.id)">
            <div class="goods-img"><van-image lazy-load :src="'http://www.dsl2023hcy.love:3004' + s.goods_img"> </van-image></div>
          </van-swipe-item>
        </van-swipe>
      </div>

      <!-- 热卖商品 -->
      <div class="hot-container">
        <van-cell title="热卖商品" />
        <div class="hot-list" v-if="hotGoods.length > 0">
          <div class="hot-item" v-for="h in hotGoods" :key="h.id" @click="toDetail(h.id)">
            <div class="goods-img">
              <div class="hot-tag">hot</div>
              <div class="goods-img"><van-image lazy-load :src="'http://www.dsl2023hcy.love:3004' + h.goods_img"> </van-image></div>
            </div>
            <!-- 商品信息 -->
            <div class="goods-content">
              <div class="goods-title">{{ h.goods_name }}</div>
              <div class="price">
                ￥ <span class="number">{{ dealPrice(h.goods_price)[0] }}</span
                >.
                <span>{{ dealPrice(h.goods_price)[1] }}</span>
                元
              </div>
            </div>
          </div>
        </div>
        <van-empty description="暂无商品数据~" v-else />
      </div>
      <!-- 最新商品 -->
      <div class="new-container">
        <van-cell title="新品上架" is-link value="更多" @click="$router.push('/new')" />
        <div class="hot-list" v-if="newGoods.length > 0">
          <div class="hot-item" v-for="n in newGoods" :key="n.id" @click="toDetail(n.id)">
            <div class="goods-img">
              <div class="hot-tag">new</div>
              <div class="goods-img"><van-image lazy-load :src="'http://www.dsl2023hcy.love:3004' + n.goods_img"></van-image></div>
            </div>
            <!-- 商品信息 -->
            <div class="goods-content">
              <div class="goods-title">{{ n.goods_name }}</div>

              <div class="price">
                ￥ <span class="number">{{ dealPrice(n.goods_price)[0] }}</span
                >.
                <span>{{ dealPrice(n.goods_price)[1] }}</span>
                元
              </div>
            </div>
          </div>
        </div>
        <van-empty description="暂无商品数据~" v-else />
      </div>
      <!-- 推荐商品 -->
      <div class="recommend-container">
        <van-cell title="精品推荐" is-link value="更多" @click="$router.push('/recommend')" />

        <div class="hot-list" v-if="recommendGoods.length > 0">
          <div class="hot-item" v-for="r in recommendGoods" :key="r.id" @click="toDetail(r.id)">
            <div class="goods-img">
              <div class="hot-tag">精</div>
              <div class="goods-img"><van-image lazy-load :src="'http://www.dsl2023hcy.love:3004' + r.goods_img"></van-image></div>
            </div>
            <!-- 商品信息 -->
            <div class="goods-content">
              <div class="goods-title">{{ r.goods_name }}</div>

              <div class="price">
                ￥<span class="number">{{ dealPrice(r.goods_price)[0] }}</span>
                <span>.{{ dealPrice(r.goods_price)[1] }}</span>
                元
              </div>
            </div>
          </div>
        </div>
        <van-empty description="暂无商品数据~" v-else />
      </div>
      <van-list offset="0" :immediate-check="false" v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <!-- 其他商品 -->
        <div class="another-container" v-if="AnotherGoods.length > 0">
          <div class="another"><van-cell title="更多商品" /></div>

          <div class="another-list">
            <goods-item v-for="an in AnotherGoods" :key="an.id" :goods_img="an.goods_img" :goods_name="an.goods_name" :goods_price="an.goods_price" :id="an.id"></goods-item>
          </div>
        </div>
        <van-empty description="暂无商品数据~" v-else />
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import axios from '@/utils/request/request.js'
import { Notify } from 'vant'
import GoodsItem from '../publicComponent/GoodsItem.vue'
export default {
  name: 'home',
  data() {
    return {
      value: '',
      swiper: [],
      recommendGoods: [],
      hotGoods: [],
      newGoods: [],
      AnotherGoods: [],
      page: 1,
      isLoading: false,
      loading: false,
      finished: false
    }
  },
  components: {
    GoodsItem
  },
  methods: {
    // 处理价格
    dealPrice(price) {
      const priceArr = price.toFixed(2).split('.')
      return priceArr
    },
    // 上拉加载
    onLoad() {
      this.page++
      this.getAnotherGoodsList()
    },
    // 下拉刷新
    async onRefresh() {
      this.page = 1
      this.AnotherGoods = []
      this.getBannerGoodsList()
      this.getRecommendGoodsList()
      this.getHotGoodsList()
      this.getNewGoodsList()
      await this.getAnotherGoodsList()
      this.isLoading = false
    },
    // 获取轮播图数据
    async getBannerGoodsList() {
      try {
        const data = await axios.get('/api/user/goods/banner')
        if (data.code === 0) {
          this.swiper = data.data
        } else {
          Notify({ type: 'danger', message: data.msg, duration: 1000 })
        }
      } catch {
        Notify({
          type: 'danger',
          message: '获取轮播图商品数据失败！',
          duration: 1000
        })
      }
    },
    // 获取店铺推荐商品
    async getRecommendGoodsList() {
      try {
        const data = await axios.get('/api/user/recommend/goods')
        if (data.code === 0) {
          // 获取数据成功
          this.recommendGoods = data.data
        } else {
          Notify({
            type: 'danger',
            message: data.msg,
            duration: 1000
          })
        }
      } catch {
        Notify({
          type: 'danger',
          message: '获取推荐商品数据失败！',
          duration: 1000
        })
      }
    },
    // 获取热卖商品
    async getHotGoodsList() {
      try {
        const data = await axios.get('/api/user/hot')
        if (data.code === 0) {
          // 获取数据成功
          this.hotGoods = data.data
        } else {
          Notify({
            type: 'danger',
            message: data.msg,
            duration: 1000
          })
        }
      } catch {
        Notify({
          type: 'danger',
          message: '获取热卖商品数据失败！',
          duration: 1000
        })
      }
    },
    // 获取最新上架商品
    async getNewGoodsList() {
      try {
        const data = await axios.get('/api/user/new/goods')
        if (data.code === 0) {
          // 获取数据成功
          this.newGoods = data.data
        } else {
          Notify({
            type: 'danger',
            message: data.msg,
            duration: 1000
          })
        }
      } catch {
        Notify({
          type: 'danger',
          message: '获取最新商品数据失败！',
          duration: 1000
        })
      }
    },
    // 获取其他商品
    async getAnotherGoodsList() {
      this.$toast.loading({
        message: '加载中...',
        duration: 0
      })
      try {
        const data = await axios.get('/api/user/goods/list', {
          params: {
            page: this.page,
            limit: 10
          }
        })
        if (data.code === 0) {
          // 获取数据成功
          this.AnotherGoods = [...this.AnotherGoods, ...data.data]
          if (data.data.length === 0 || data.data.length < 10) {
            this.finished = true
          }
        } else {
          Notify({
            type: 'danger',
            message: data.msg,
            duration: 1000
          })
        }
      } catch {
        Notify({
          type: 'danger',
          message: '获取更多商品数据失败！',
          duration: 1000
        })
      } finally {
        this.loading = false
        this.$toast.clear()
      }
    },
    // 点击搜索按钮
    onSearch() {
      if (this.value === '') return Notify({ type: 'warning', message: '请输入搜索关键词！', duration: 1000 })
      this.$router.push('/search?keyword=' + this.value)
    },
    // 前往商品详情页
    toDetail(id) {
      this.$router.push('/goods/detail/' + id)
    }
  },
  created() {
    this.getBannerGoodsList()
    this.getRecommendGoodsList()
    this.getHotGoodsList()
    this.getNewGoodsList()
    this.getAnotherGoodsList()
  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 46px;
  padding-bottom: 50px;
  min-height: 100vh;
  background-color: #f5f5f5;
  .header {
    position: fixed;
    top: 0;
    left: 0;
    height: 46px;
    border-bottom: 1px solid #f1f1f1;
    width: 100vw;
    z-index: 999;

    ::v-deep .van-search {
      padding: 5px 12px;
    }

    ::v-deep .van-search__action {
      padding: 0 0 0 8px;
    }
    .search-btn {
      background-color: #ee0a24;
      border-radius: 999px;
      color: #fff;
      width: 50px;
      height: 24px;
      text-align: center;
      line-height: 24px;
    }
  }

  .swiper {
    .goods-img {
      width: 100vw;

      ::v-deep .van-image {
        width: 100%;
        height: auto;
      }

      img {
        width: 100%;
        height: auto;
      }
    }
  }

  ::v-deep .van-cell__title {
    font-size: 18px;
    span {
      font-weight: 700;
    }
  }

  .recommend-container,
  .another-container,
  .new-container,
  .hot-container {
    margin-top: 10px;
    padding-bottom: 5px;

    .goods-img {
      display: flex;
      align-items: center;
      justify-content: center;
      min-height: 100px;
      min-width: 100px;
    }
  }

  .recommend-container .hot-tag {
    border-bottom-left-radius: 0 !important;
    padding: 1px 3px !important;
  }
  .hot-list {
    display: flex;
    flex-wrap: nowrap;
    overflow: auto;
    background-color: #f1f1f1;

    .hot-item {
      flex-shrink: 0;
      width: 120px;
      margin: 0 10px;
      background-color: #fff;

      .goods-img {
        position: relative;

        .hot-tag {
          position: absolute;
          right: 0;
          top: 0;
          padding: 1px 3px 1px 10px;
          background-color: #ee0a24;
          color: #fff;
          font-size: 12px;
          z-index: 2;
          font-weight: 700;
          border-bottom-left-radius: 15px;
        }
      }

      .goods-content {
        background-color: #fff;
        padding: 0 5px;
        .goods-title {
          margin-top: 10px;
          font-size: 14px;
          font-weight: 600;
          overflow: hidden;
          text-overflow: ellipsis; /*显示为......*/
          -webkit-line-clamp: 2; /*显示为两行，在第二行中显示为.......*/
          display: -webkit-box;
          -webkit-box-orient: vertical;
        }

        .price {
          margin-top: 5px;
          font-size: 12px;
          color: #ee0a24;
          padding-bottom: 10px;
          span {
            font-weight: 700;
          }

          .number {
            font-size: 16px;
          }
        }
      }
    }
  }
  ::-webkit-scrollbar {
    display: none;
  }

  .another-container {
    background-color: #f1f1f1;

    .another {
      background-color: #fff;
    }
    .another-list {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      padding: 0 10px;
    }
  }
}
</style>
